package com.dartfar.dictionary.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.dartfar.base.action.BaseAction;
import com.dartfar.dictionary.model.Dictionary;
import com.dartfar.dictionary.service.DictcatalogService;
import com.dartfar.dictionary.service.DictionaryService;
import com.dartfar.dictionary.vo.DictionaryVo;
import com.dartfar.util.DictionaryFactory;

@Scope("prototype")
@Component("dictionaryAction")
public class DictionaryAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Service对象
	 */
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	private DictcatalogService dictcatalogService;

	/**
	 * 页面传参
	 */
	private int id;
	private int dclId;
	private String dictKey;
	private String dictWord;
	
	private String inserted;
	private String updated;
	private String deleted;
	
	/**
	 * 页面跳转
	 */
	public String show() {
		//获取权限
		functions = getAutFunctions();
		return "show";
	}
	
	
	/**
	 * 保存 综合操作
	 * @param inserted, updated, deleted
	 * @return null
	 */
	public String save(){
		
		List<DictionaryVo> list;
		//新建键值
		if(inserted != null){
			//把JSON字符串转换成对象
			list = JSON.parseArray(inserted, DictionaryVo.class);
			for(Iterator<DictionaryVo> i =  list.iterator();i.hasNext();){
				DictionaryVo dict = i.next();
				dictKey = dict.getKey();
				dictWord = dict.getWord();
				dclId = dict.getDclId();
				add();
			}
		}
		//更新键值
		if(updated != null){
			//把JSON字符串转换成对象
			list = JSON.parseArray(updated, DictionaryVo.class);
			for(Iterator<DictionaryVo> i =  list.iterator();i.hasNext();){
				DictionaryVo dict = i.next();
				id = dict.getId();
				dictKey = dict.getKey();
				dictWord = dict.getWord();
				dclId = dict.getDclId();
				edit();
			}
		}
		//删除键值
		if(deleted != null){
			//把JSON字符串转换成对象
			list = JSON.parseArray(deleted, DictionaryVo.class);
			for(Iterator<DictionaryVo> i =  list.iterator();i.hasNext();){
				DictionaryVo dict = i.next();
				id = dict.getId();
				remove();
			}
		}
		//重新加载数据字典
		reload();
		return null;
	}
	
	public String add(){
		Dictionary dictionary = new Dictionary();
		dictionary.setD_key(dictKey);
		dictionary.setD_word(dictWord);
		dictionary.setDictCatalog(dictcatalogService.get(dclId));
		dictionaryService.save(dictionary);
		return null;
	}
	
	public String edit(){
		Dictionary dictionary = dictionaryService.get(id);
		dictionary.setD_key(dictKey);
		dictionary.setD_word(dictWord);
		System.out.println("dclId: " + dclId);
		dictionary.setDictCatalog(dictionary.getDictCatalog());
		dictionaryService.update(dictionary);
		return null;
	}
	
	public String remove(){
		dictionaryService.delete(dictionaryService.get(id));
		return null;
	}
	
	public void reload(){
		DictionaryFactory.getInstance().reloadDictMap();  
	}
	
	public String loadDictionaryData(){
		List<DictionaryVo> dictionaryVos = new ArrayList<DictionaryVo>();
		try{
		if (curUser() != null){
			List<Dictionary> list = dictionaryService.getListByDictCatalog(dclId);	//查看全部
			for(Iterator<Dictionary> i =  list.iterator();i.hasNext();){
				Dictionary n = i.next();
				DictionaryVo dictionaryVo = new DictionaryVo();
				dictionaryVo.setId(n.getId());
				dictionaryVo.setKey(n.getD_key());
				dictionaryVo.setWord(n.getD_word());
				dictionaryVo.setDclId(dclId);
				dictionaryVos.add(dictionaryVo);
			}			
		}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		String jsonResult = JSONArray.fromObject(dictionaryVos).toString();
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}


	/**
	 *  Gets And Sets
	 */
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}

	public String getDictKey() {
		return dictKey;
	}

	public void setDictKey(String dictKey) {
		this.dictKey = dictKey;
	}

	public String getDictWord() {
		return dictWord;
	}

	public void setDictWord(String dictWord) {
		this.dictWord = dictWord;
	}

	public int getDclId() {
		return dclId;
	}

	public void setDclId(int dclId) {
		this.dclId = dclId;
	}

	public String getInserted() {
		return inserted;
	}

	public void setInserted(String inserted) {
		this.inserted = inserted;
	}

	public String getUpdated() {
		return updated;
	}

	public void setUpdated(String updated) {
		this.updated = updated;
	}

	public String getDeleted() {
		return deleted;
	}

	public void setDeleted(String deleted) {
		this.deleted = deleted;
	}
	
 }
